import Schema from 'async-validator';
function handleErrors(errors, fields) {
  console.log("error: ", errors, fields)
}
const descriptor = {
  name: {
    type: 'string',
    required: true,
    validator: (rule, value) => value === 'muji',
    message: 'Value is not equal to "test".'
  },
  age: {
    type: 'number',
    asyncValidator: (rule, value) => {
      return new Promise((resolve, reject) => {
        if (value < 18) {
          reject('too young');  // reject with error message
        } else {
          resolve();
        }
      });
    },
  },
  score:{
    validator:(value)=>value > 60,
    message: 'bu ji ge'
  },
  lover:{
    required:true
  },
  kk:{
    
  }
};
const validator = new Schema(descriptor);
validator.validate({ namex: 'muj2i', age: 16 }).then(() => {
  // validation passed or without error message
}).catch(({ errors, fields }) => {
  return handleErrors(errors, fields);
});

class MySchema{
  constructor(descriptor){
    this.descriptor = descriptor;
  }
  validate(tableData){

    Object.keys(this.descriptor)
    .forEach((key)=>{
      // 检测每一个分项
      const item = tableData.find(item=>item.label == key)
      if(item){
        // 存在属性
        if(!this.descriptor[key].validator(item.value)){
          // 检验没通过
          item.errorMsg = this.descriptor[key].message;
        }
      }else{
        // 不存在属性
        if(this.descriptor[key].required){
          const errorMsg = key + ' is required';
          tableData.push({
            errorMsg,
            label:key
          })
        }
      }
      
    })
  }
}

const tableData = [{
  label:'name',
  value:'li',
},
{
  label:'score',
  value:14,
}
]
const s = new MySchema(descriptor)
s.validate(tableData)
console.log(tableData)